package com.lsh.oms.api.model.order.receipt;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.lsh.base.lang.Maps;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author peter
 */
@Setter
@Getter
@NoArgsConstructor
public class ReceiptHeadDto implements Serializable {
    /**
     *
     */
    private static final long serialVersionUID = -2511354919669380353L;

    private Long shippingOrderId;

    private List<ReceiptDetailDto> details;

    private Map<String, Object> party;

    public String getSortdStr() {
        StringBuilder sb = new StringBuilder();
        sb.append("order:").append(shippingOrderId);
        if (CollectionUtils.isNotEmpty(details)) {
            Collections.sort(details);
            for (ReceiptDetailDto receiptDetailDto : details) {
                sb.append("skuId:").append(receiptDetailDto.getSkuId()).append("qty:").append(receiptDetailDto.getQty());
            }
        }
        return sb.toString();
    }

    public List<Map<String, Object>> toCalServiceSukList() {
        return Optional.ofNullable(details).map(details -> details.stream().filter($ -> $.getQty().compareTo(BigDecimal.ZERO) >= 0).map($ -> Maps.of("sku_id", $.getSkuId(), "item_code", $.getItemCode(), "qty", $.getQty())).collect(Collectors.toList())).orElse(Collections.emptyList());
    }
}